﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using XmlStudio.Helper;

namespace XmlStudio.Controls {
    /// <summary>
    /// SpectrumSlider class.
    /// </summary>
    public class SpectrumSlider : Slider {
        /// <summary>
        /// Initializes static members of the SpectrumSlider class.
        /// </summary>
        static SpectrumSlider() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SpectrumSlider), new FrameworkPropertyMetadata(typeof(SpectrumSlider)));
        }

        #region Public Properties
        public Color SelectedColor {
            get {
                return (Color)GetValue(SelectedColorProperty);
            }
            set {
                SetValue(SelectedColorProperty, value);
            }
        }
        #endregion

        #region Dependency Property Fields
        public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register("SelectedColor", typeof(Color), typeof(SpectrumSlider),
            new PropertyMetadata(Colors.Transparent));
        #endregion


        #region Public Methods
        public override void OnApplyTemplate() {
            base.OnApplyTemplate();
            mSpectrumDisplay = GetTemplateChild(SpectrumDisplayName) as Rectangle;
            UpdateColorSpectrum();
            OnValueChanged(Double.NaN, Value);
        }
        #endregion

        #region Protected Methods
        protected override void OnValueChanged(double oldValue, double newValue) {
            base.OnValueChanged(oldValue, newValue);
            Color theColor = ColorUtilities.ConvertHsvToRgb(360 - newValue, 1, 1);
            SetValue(SelectedColorProperty, theColor);
        }
        #endregion

        #region Private Methods
        private void UpdateColorSpectrum() {
            if(mSpectrumDisplay != null) {
                CreateSpectrum();
            }
        }

        private void CreateSpectrum() {
            pickerBrush = new LinearGradientBrush {
                                                      StartPoint = new Point(0.5, 0),
                                                      EndPoint = new Point(0.5, 1),
                                                      ColorInterpolationMode =
                                                          ColorInterpolationMode.SRgbLinearInterpolation
                                                  };

            List<Color> colorsList = ColorUtilities.GenerateHsvSpectrum();
            double stopIncrement = (double)1 / colorsList.Count;

            int i;
            for(i = 0; i < colorsList.Count; i++) {
                pickerBrush.GradientStops.Add(new GradientStop(colorsList[i], i * stopIncrement));
            }

            pickerBrush.GradientStops[i - 1].Offset = 1.0;
            mSpectrumDisplay.Fill = pickerBrush;

        }
        #endregion

        #region Private Fields
        private const string SpectrumDisplayName = "PART_SpectrumDisplay";
        private Rectangle mSpectrumDisplay;
        private LinearGradientBrush pickerBrush;
        #endregion
    }
}
